package com.bingo.base;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * 发送异步消息
 */
@Component
public class AsyncProducer {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    public void send(String payload) {
        Message<String> message = MessageBuilder.withPayload(payload).build();
        rocketMQTemplate.asyncSend("Base:springboot-async", message, new SendCallback() {
            /**
             * 发送成功回调函数
             * @param sendResult
             */
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送结果：" + sendResult);
            }

            /**
             * 发送失败回调函数
             * @param e
             */
            public void onException(Throwable e) {
                System.out.println("发送异常：" + e);
            }
        });
    }
}
